package com.douya.security.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 安全工具类
 *
 * @Author hcy
 * @Date 2022/11/23
 */
public class SecurityUtils {

    /**
     * 获取当前用户的认证信息
     *
     * @param t   Class
     * @param <T> 注入SecurityContextHolder.getContext().getAuthentication()的类型
     * @return T
     */
    @SuppressWarnings("unchecked")
    public static <T> T getCurrentUserDetail(Class<T> t) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }
        return authentication.getPrincipal() instanceof String ? null : (T) authentication.getPrincipal();
    }
}
